'use client'

import { QueryClient } from '@tanstack/react-query'

// 创建 QueryClient 实例
export const queryClient = new QueryClient({
  defaultOptions: {
    queries: {
      // 数据缓存时间
      staleTime: 5 * 60 * 1000, // 5分钟
      // 垃圾回收时间
      gcTime: 10 * 60 * 1000, // 10分钟
      // 重试次数
      retry: 2,
      // 重试延迟
      retryDelay: (attemptIndex) => Math.min(1000 * 2 ** attemptIndex, 30000),
      // 窗口失焦时不重新获取数据
      refetchOnWindowFocus: false,
      // 网络重连时重新获取数据
      refetchOnReconnect: true,
    },
    mutations: {
      retry: 1,
    },
  },
})

// 查询键常量
export const QUERY_KEYS = {
  // 用户相关
  USER: ['user'] as const,
  USER_PROFILE: (id: string) => ['user', id] as const,

  // 医院相关
  HOSPITALS: ['hospitals'] as const,
  HOSPITAL: (id: string) => ['hospital', id] as const,
  HOSPITAL_BUILDINGS: (hospitalId: string) => ['hospital', hospitalId, 'buildings'] as const,

  // 科室相关
  DEPARTMENTS: ['departments'] as const,
  DEPARTMENT: (id: string) => ['department', id] as const,

  // 设备相关
  EQUIPMENT: ['equipment'] as const,
  EQUIPMENT_BY_DEPARTMENT: (departmentId: string) => ['equipment', 'department', departmentId] as const,

  // 实时数据
  REAL_TIME_DATA: ['realTimeData'] as const,
  HEATMAP_DATA: ['heatmapData'] as const,
  ALERT_DATA: ['alertData'] as const,

  // 图表数据
  CHART_DATA: (type: string) => ['chartData', type] as const,
  TREND_DATA: (timeRange: string) => ['trendData', timeRange] as const,

  // 3D 模型数据
  MODEL_DATA: (modelId: string) => ['modelData', modelId] as const,
  POI_DATA: (buildingId: string) => ['poiData', buildingId] as const,
} as const